<%@ page language="java"  pageEncoding="utf-8"%>
<%@page import="cn.biplam.back.jda.Jda"%>
<%@page import="cn.biplam.common.client.DbResource"%>
<%@page import="cn.biplam.common.wrapper.ErrorMessage"%>
<%@page import="cn.biplam.common.bean.sqlengine.Sql"%>
<%@page import="cn.biplam.common.wrapper.sqlresult.SqlResult"%>
<%@page import="cn.biplam.common.wrapper.sqlresult.SqlResultMetaData"%>
<%@page import="cn.biplam.common.tools.TOOLS"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>数据库导入</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<style>
		body {font-size:12px;font-family:宋体}
		ul.TabBarLevel1{
			list-style:none;
			margin:0;
			padding:0;
			height:29px;
			background:#effece;
		}
		ul.TabBarLevel1 li{
			float:left;
			padding:0;
			height:29px;
			margin-right:1px;
			background:#effece;
		}
		ul.TabBarLevel1 li a{
			display:block;
			line-height:29px;
			padding:0 20px;
			color:#333;
			background:#effece;
			white-space: nowrap;
		}
		ul.TabBarLevel1 li.Selected{
			background:#effece;
		}
		ul.TabBarLevel1 li.Selected a{
			background:#effffe;
		}
		
		ul.TabBarLevel1 li a:link,ul.TabBarLevel1 li a:visited{
			color:#333;
		}
		ul.TabBarLevel1 li a:hover,ul.TabBarLevel1 li a:active{
			color:#F30;
			text-decoration:none;
		}
		ul.TabBarLevel1 li.Selected a:link,ul.TabBarLevel1 li.Selected a:visited{
			color:#000;
		}
		ul.TabBarLevel1 li.Selected a:hover,ul.TabBarLevel1 li.Selected a:active{
			color:#F30;
			text-decoration:none;
		}
		div.HackBox {
		  height:220px;
		  padding : 1px 1px ;
		  border-top: 1px solid #6697CD;
		  border-left: 1px solid #6697CD;
		  border-right: 1px solid #6697CD;
		  border-bottom: 1px solid #6697CD;
		  clear:both;
		}
		
	</style>
	<script language="JavaScript">
			var cId="-1";
			function switchTab(tabpage,x){
			    document.myform.action="sql.jsp?show="+x.id;
			    document.forms.myform.submit();
			}
			
			function changeBg(x){
				x.style.fontWeight="300";
				x.style.cursor="hand";
				if(!x.onmouseout){
					x.onmouseout = function(){
						x.style.fontWeight="normal";
					};
				}
			}
			/**全选*/
			function selectAll(){ 
			   var a = document.getElementsByName("sid"); 
			   if(document.getElementById("sidAll").checked==false){ 
				   for (var i=0; i<a.length; i++) {
				      if (a[i].type == "checkbox") a[i].checked = false; 
				   }
				}else{ 
					for (var i=0; i<a.length; i++){ 
					      if (a[i].type == "checkbox") a[i].checked = true; 
					}
			    } 
			} 
		
		</script>
  </head>
  
  <body>
     <form name="myform" action="" method="post">
	    <div id="Whatever">
			<ul class="TabBarLevel1" id="TabPage1">
				<li id="exeSQL" onmouseover="changeBg(this)" onclick="javascript:switchTab('TabPage1',this);"><a>Select执行器</a></li>
			    <li id="exeDDL" onmouseover="changeBg(this)" onclick="javascript:switchTab('TabPage1',this);"><a>DDL执行器</a></li>
			</ul>
			<%
				//执行sql
				String sql = "";
				sql = request.getParameter("sql");
				SqlResult sr = null;
				String prompt = null;
				String type = null;
				String ddlPromt = null;
				boolean success = true;
				StringBuilder sb = new StringBuilder();
				try {

					if (request.getParameter("show") == null
							|| "exeSQL".equals(request.getParameter("show"))) {
						prompt = "执行查询";
						type = "t";
					} else if ("exeDDL".equals(request.getParameter("show"))) {
						prompt = "执行DDL";
						type = "d";
					} else {
					    type = request.getParameter("type");
					    if("t".equals(request.getParameter("type"))){
					    	prompt = "执行查询";
					    }else{
					    	prompt = "执行DDL";
					    }
						if (sql != null && !"".equals(sql.trim())) {
							DbResource mDbResource = new DbResource("RT");
							Jda mJda = new Jda(mDbResource);
							if ("t".equals(request.getParameter("type"))) {
								sr = mJda.executeSelect(new Sql(sql));
							} else if ("d".equals(request.getParameter("type"))) {
								mJda.executeDDL(new Sql(sql));
								ddlPromt="执行成功";
							}
						}
					}

				} catch (ErrorMessage e) {
					ddlPromt = "sql错误";
					success = true;
					e.getDescription(sb);	
				}
			%>
			<div id="content" class="HackBox">
			   
			   <textarea rows="15" cols="100%" name="sql" id="sql" title="ttt"><%=sql%></textarea>
			   <input type="button" value="<%=prompt%>" onclick="javascript:switchTab('TabPage1',this);">
			   <input type="hidden" name="type" value="<%=type%>">
			</div>
			
			<div id="content" class="HackBox">
			       <%
  			      	if (sr != null && sr.size() > 0) {
  			      		String result = TOOLS.getTable(sr);
  			      %>  
			       <%=result%>
			       <%
			       	}else{
			       %>
			       <%=ddlPromt%><br>
			       <%=sb.toString()%>
			       <%
			       	}
			       %>
			       
			       
			</div>
		</div>
	</form>
  </body>
</html>
